package com.adminx.security;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adminx.dao.AccountDao;
import com.adminx.pojo.MenuPojo;

/**
 * Spring Security 登陆成功后的跳转工具
 *
 */
@Service
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class LoginSuccessHandle implements AuthenticationSuccessHandler{
	
	@Autowired
	private AccountDao accountDao;
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		
		String path = request.getContextPath() ;
		String basePath = request.getScheme()+"://"+request.getServerName()+":" +request.getServerPort()+path+"/";

		List<MenuPojo> menu = this.accountDao.findMenuByAuthority();
		
		HttpSession session = request.getSession();
		session.setAttribute("menu", menu);
		response.sendRedirect(basePath+"index.html");
	}
}
